package openadmin.widgets.swing;

import java.awt.Cursor;
import java.awt.Dimension;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.swing.JButton;


/**
 * Template Button
 * An implementation of a "push" button
 * @version  0.1
 * Created 10-05-2009
 * Author Alfred Oliver
 */
public class TemplateJbuttont01 extends JButton {
	
	Class<?> classe = null;
	Method m = null;
	Object objecte;
	
	/**	  	
	  * Constructs a new Button with the specified string of text, and object and method to invoke. 
	  * @param pText. Sets the text for this button. 
	  * @param pObject Object containing the method to invoke  	 
	  * @param pMethod Method to invoke. 	  
	  */
	public TemplateJbuttont01(String pText, final Object pObject, final String pMethod) {
						
		super(pText);
		this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		this.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 102)));
		
		this.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		this.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		this.setPreferredSize(new Dimension(150, -1)); 
		
		this.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	            	
	            	setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
	            	executaMethod(pObject, pMethod);
	            	setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));	            	
	        
	            }
		});
	
		this.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
            	
            	setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
            	executaMethod(pObject, pMethod);
            	setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));	
            
            }
        });     
				
	}

	//Invoke method
	private void executaMethod(final Object pObject, final String pMethod){
		
		try {
			
			classe = Class.forName(pObject.getClass().getName());	        				
			m = classe.getMethod(pMethod, null);
			m.invoke(pObject, null);
			
		} catch (IllegalArgumentException e) {
			
			e.printStackTrace();
		
		} catch (IllegalAccessException e) {
			
			e.printStackTrace();
			
		} catch (InvocationTargetException e) {
			
			e.printStackTrace();
			
		} catch (ClassNotFoundException e1) {
			
			e1.printStackTrace();
			
		} catch (SecurityException e1) {
			
			e1.printStackTrace();
		} catch (NoSuchMethodException e1) {
			
			e1.printStackTrace();
		} 
		        			            	                
	}
		
}
